package com.hqs.listener.utils;

import android.os.Environment;

import com.hqs.common.utils.Log;
import com.hqs.listener.app.Application;
import com.jieya.cn.UnRar;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 16/9/26.
 */

public class FileUtil {


    public static String userDocumentPath(){
        String path;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            path = Environment.getExternalStorageDirectory().getAbsolutePath();
            path += "/listener/";
            File file = new File(path);
            if (!file.exists()){
                boolean ok = file.mkdir();
                Log.print(ok);
            }
            return path;
        }
        path = Application.getInstance().getFilesDir().getAbsolutePath();
        Log.print(path);
        return path + "/";
    }

    public static void makeDir(String fileDir){
        File file = new File(fileDir);
        if (!file.exists()){
            file.mkdirs();
        }
    }

    public static String dataDir(){
        String dir = userDocumentPath() + "/data/";
        File file = new File(dir);
        if (!file.exists()){
            file.mkdir();
        }
        return dir;
    }

    public static boolean fileExsits(String filePath){
        File file = new File(filePath);
        return file.exists();
    }

    public static String logDir(){
        String dir = userDocumentPath() + "/log/";
        File file = new File(dir);
        if (!file.exists()){
            file.mkdir();
        }
        return dir;
    }

    public static String backupFilePath(){
        String dir = dataDir();
        String filePath = dir + "backup.json";
        return filePath;
    }

    public static String fileToString(File file){
        StringBuffer stringBuffer = new StringBuffer();

        try {
            FileInputStream fis = new FileInputStream(file);

            int len = 0;
            byte[] bytes = new byte[1024 * 4];
            while (true){
                len = fis.read(bytes);
                if (len == - 1){
                    break;
                }
                else {
                    stringBuffer.append(new String(bytes, 0, len));
                }
            }
            fis.close();
        } catch (Exception e) {
        }

        return stringBuffer.toString();
    }

    public static void removeDir(File file){
        if (file.isDirectory()){
            File[] files = file.listFiles();
            for (File f: files){
                removeDir(f);
            }
        }
        else{
            file.delete();
        }
    }

    public static void clearDir(File file){
        if (file.isDirectory()){
            File[] files = file.listFiles();
            for (File f: files){
                removeDir(f);
            }
        }
    }

    public static void clearDir(String filePath){
        File file = new File(filePath);
        if (file.isDirectory()){
            File[] files = file.listFiles();
            for (File f: files){
                removeDir(f);
            }
        }
    }

    public static void removeFiles(List<File> files){
        if (files != null) {
            int len = files.size();
            if (len > 0){
                for (File file: files){
                    file.delete();
                }
            }
        }
    }

    public static String fileSize(long size){
        double g = size / 1024.0 / 1024 / 1024;
        String s = "";
        if (g > 1) {
            s = String.format("%.02f", g) + "GB";
            return s;
        }

        double m = size / (1024.0 * 1024);
        if (m > 1) {
            s = String.format("%.02f", m) + "MB";
            return s;
        }

        double k = size / 1024.0;
        if (k > 1){
            s = String.format("%.02f", k) + "KB";
            return s;
        }

        return s + "B";
    }

    public static long fileSize(String filePath){
        return fileSize(new File(filePath));
    }

    public static long fileSize(File file){
        if (file != null && file.exists() && file.isFile()){
            return file.length();
        }
        return 0;
    }

    public static long filesTotalSize(ArrayList<String> filePaths){

        long size = 0;
        for (String p : filePaths){
            size += fileSize(p);
        }
        return size;
    }



    public static ArrayList<File> findFiles(String dir){
        File file = new File(dir);
        ArrayList<File> files = new ArrayList<>();
        findFiles(file, files);
        return files;
    }

    private static void findFiles(File dir, ArrayList<File> fileArrayList){
        if (dir.isDirectory()){
            File[] files = dir.listFiles();
            for (File file : files){
                if (file.isDirectory()){
                    findFiles(file, fileArrayList);
                }
                else{
                    fileArrayList.add(file);
                }
            }
        }
        else{
            fileArrayList.add(dir);
        }
    }

    public static boolean isImageFile(String file){
        if(file == null){
            return false;
        }
        String image = ".bmp.jpg.png.tiff.gif.pcx.tga.exif.fpx.svg.psd.cdr.pcd.dxf.ufo.eps.ai.raw.WMF";
        String suffix = getFileSuffix(file);
        return image.contains(suffix);
    }

    public static String getFileSuffix(String file){
        if (file == null) {
            return "";
        }
        return file.substring(file.lastIndexOf("."));
    }

    public static String getFileDir(String file){
        if (file != null) {
            int index = file.lastIndexOf(File.separator);
            if (index != -1){
                return file.substring(0, index + 1);
            }
        }
        return "";
    }

    public static boolean saveToFile(String data, String filePath){
        File f = new File(filePath);
        try {
            if (!f.exists()){
                f.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(f, false);
            fos.write(data.getBytes("UTF-8"));
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean unRar(String rar, String dir){
        UnRar a = new UnRar();
        try {
            boolean isOk = a.unrar(rar, dir);
            return isOk;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}


